import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class JButtonMoveFrame extends JFrame {
    private JButton objT1;
    public JButtonMoveFrame() {
        super("小游戏");
        this.setSize(500,500);
        this.setLayout(null);
        objT1 = new JButton("夏宇阳");
        objT1.setBounds(120, 40, 10, 10);
        this.add(objT1);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



        KeyAdapter kl = new KeyAdapter(){
            @Override
            public void keyPressed(KeyEvent e){
                JButton btn=(JButton)e.getSource();

                if(e.getKeyCode()==e.VK_UP){
                    btn.setBounds(btn.getX(), btn.getY() - 20, 50, 40);
                } else if(e.getKeyCode()==e.VK_DOWN){
                    btn.setBounds(btn.getX(), btn.getY() + 20, 50, 40);
                } else if(e.getKeyCode()==e.VK_LEFT){
                    btn.setBounds(btn.getX() - 20, btn.getY(), 50, 40);
                } else if(e.getKeyCode()==e.VK_RIGHT){
                    btn.setBounds(btn.getX() + 20, btn.getY(), 50, 40);
                }
            }
        };
        objT1.addKeyListener(kl);
    }
    public static void main(String[] args) {
        new JButtonMoveFrame();
    }
}